//
//  main.m
//  03-Foundation-NSMutableString
//
//  Created by Jason on 15/9/28.
//  Copyright © 2015年 Jason. All rights reserved.
//

#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        
        NSMutableString *str = [NSMutableString stringWithFormat:@"/user/jason/abc.txt"];
        NSRange range = [str rangeOfString:@"abc"];
        [str replaceCharactersInRange:range withString:@"ccc"];
//        NSLog(@"str=%@", str);
        
        
        
        
        NSInteger location = [str rangeOfString:@"/jason"].location;
        [str insertString:@"/jj" atIndex:location];
         NSLog(@"str=%@", str);
    }
    return 0;
}

#pragma mark 可变字符串删除
void stringDelete() {
    NSMutableString *str = [NSMutableString stringWithFormat:@"user/jason/apple/jason.xxx"];
    // 默认返回的是第一个jason的范围
    //        NSRange range = [str rangeOfString:@"jason"];
    // NSBackwardsSearch 从字符串尾部开始搜索内容
    NSRange  range = [str rangeOfString:@"jason" options:NSBackwardsSearch];
    
    // 在某个范围搜索xxx字符串删除
    //        [str rangeOfString:<#(nonnull NSString *)#> options:<#(NSStringCompareOptions)#> range:<#(NSRange)#>]
    
    // 删除jason
    [str deleteCharactersInRange:range];
    
    NSLog(@"str=%@", str);
}

#pragma mark 可变字符串的拼接
void stringAppend() {
    
    NSString *qq = @"992323";
    NSString *pwd = @"dasdas";
    // 拼接url地址
    //        NSString *url = [NSString stringWithFormat:@"http://im.qq.com?qq=%@&pwd=%@",qq,pwd];
    NSMutableString *url = [NSMutableString stringWithFormat:@"http://im.qq.com?"];
    [url appendFormat:@"qq=%@", qq];
    [url appendFormat:@"&"];
    [url appendFormat:@"pwd=%@",pwd];
    NSLog(@"%@", url);
}

#pragma mark 可变字符串的简单实用
void stringUse() {
    //        NSMutableString *str = @"123123"; 这种写法不可取 NSMutableString是可变的字符串 后面是不可变的字符串
    
    NSMutableString *str = [NSMutableString stringWithFormat:@"Jason"];
    
    // 拼接到str的后面
    [str appendString:@"And Rose"];
    
    [str appendFormat:@" age is %d and height is %f", 20, 1.78];
    
    NSLog(@"str=%@",str);
    
    /*
     NSString *str1 = @"Jack";
     NSString *str2 = [str1 stringByAppendingString:@"and Mike"]; // 该方法是返回了一个新对象
     NSLog(@"str1=%@",str1);
     NSLog(@"str2=%@",str2);
     */
}
